package com.hex.ds.code.detection.pack.utils;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;

/**
 * @Package com.hex.ds.code.detection.script.pack.utils
 * @ClassName GitUtil
 * @Description Git相关工具类
 * @Author Wang zhihao
 * @Date 2024/1/18 13:16
 * @Version v1.0
 **/
public class GitUtil {

    /**
     * @Method cloneGit
     * @Param
     * @param gitUrl
     * @param branchName
     * @param localDir
     * @param username
     * @param password
     * @Return void
     * @Description 克隆指定git分支的代码到本地
     * @Author Wang zhihao
     * @Date 2024/1/18 13:26
     * @Version V1.0
     */
    public static void cloneGit(String gitUrl, String branchName, String localDir, String username, String password) {
        try {
            Git.cloneRepository()
                    .setURI(gitUrl)
                    .setDirectory(new File(localDir))
                    .setBranch(branchName)
                    .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                    .call();
        } catch (Exception e){
            throw new RuntimeException("克隆指定git分支的代码到本地异常: " + e.getMessage(), e);
        }
    }
}
